package com.smap.vector;

import java.util.LinkedHashMap;

import com.smap.core.Map;
import com.smap.core.Projection;
import com.smap.core.XY;

public class Feature {

	// 0 = normal, 1 = creating, 2 = modifying
	public int status = 0;

	public LinkedHashMap<String, String> attr = new LinkedHashMap<String, String>();
	public Geometry geom;
	public Style style = new Style();

	LinkedHashMap<String, Geometry> cache = new LinkedHashMap<String, Geometry>();

	public Geometry screenGeom;

	public void label(String label) {
		this.attr.put("label", label);
	}

	public Geometry pack(Map map) {
		XY centroid = map.getCentre();
		double res = map.getResolution();
		String key = centroid + "-" + res;
		screenGeom = cache.get(key);
		if (screenGeom == null) {
			screenGeom = GeomUtils.clone(this.geom);
			for (XY p : screenGeom.coordinates) {
				p.x = ((p.x - centroid.x) / res);
				p.y = -((p.y - centroid.y) / res);
			}
			cache.put(key, screenGeom.pack());
		}
		return screenGeom;
	}

	public void project(Projection proj) {
		for (XY xy : this.geom.coordinates) {
			proj.project(xy);
		}
		this.geom.pack();
	}

	public void reversePack(Map map) {
		XY centroid = map.getCentre();
		double res = map.getResolution();
		geom = GeomUtils.clone(this.screenGeom);
		for (XY p : geom.coordinates) {
			p.x = centroid.x + p.x * res;
			p.y = centroid.y - p.y * res;
		}
	}

	public void screenToCoord(Map map, XY p) {
		XY centroid = map.getCentre();
		double res = map.getResolution();
		geom = GeomUtils.clone(this.screenGeom);
		p.x = centroid.x + p.x * res;
		p.y = centroid.y - p.y * res;
	}

}
